package com.wmg.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//保证序列化json的时候,如果是null的对象,key也会消失
public class JsonResult<T> implements Serializable {

    private Logger log = LoggerFactory.getLogger(JsonResult.class);

    private int status;
    private String msg;
    private T data;

    public static ObjectMapper objectMapper;

    static {
        objectMapper = new ObjectMapper();
    }

    private JsonResult(int status) {
        this.status = status;
    }

    private JsonResult(int status, T data) {
        this.status = status;
        this.data = data;
    }

    private JsonResult(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    private JsonResult(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    @JsonIgnore
    //使之不在json序列化结果当中
    public boolean isSuccess() {
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    public int getStatus() {
        return status;
    }

    public T getData() {
        return data;
    }

    public String getMsg() {
        return msg;
    }

    public static <T> JsonResult<T> createSuccess() {
        return new JsonResult<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> JsonResult<T> createSuccessWithMessage(String msg) {
        return new JsonResult<T>(ResponseCode.SUCCESS.getCode(), msg);
    }

    public static <T> JsonResult<T> createSuccessWithData(T data) {
        return new JsonResult<T>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> JsonResult<T> createSuccessWithAll(String msg, T data) {
        return new JsonResult<T>(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> JsonResult<T> createErrorByCode(ResponseCode errorCode) {
        return new JsonResult<T>(errorCode.getCode(), errorCode.getDesc());
    }

    public static <T> JsonResult<T> createErrorByMessage(String errorMessage) {
        return new JsonResult<T>(ResponseCode.ERROR.getCode(), errorMessage);
    }

    public static <T> JsonResult<T> createErrorCodeMessage(int errorCode, String errorMessage) {
        return new JsonResult<T>(errorCode, errorMessage);
    }

    /**
     * 返回json字符串
     *
     * @param pretty 是否格式化
     * @return
     * @throws JsonProcessingException
     */
    public String JsonString(boolean pretty) {
        try {
            return pretty ? objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this)
                    : objectMapper.writeValueAsString(this);
        } catch (JsonProcessingException exception) {
            log.error("json构造错误,{}", exception);
            return "{}";
        }
    }

    public String JsonString() {
        return JsonString(false);
    }

    public static void main(String[] args) {
        ObjectMapper objectmapper = new ObjectMapper();
        String str = JsonResult.createErrorCodeMessage(10, "登陆").JsonString(true);
        //objectmapper.writeValueAsString(JsonResult.createErrorCodeMessage(10, "denglu"));
        System.out.println(str);
    }
}
